package com.manyauthenticationway.server.manyauthenticationway.config.oauth2

import com.manyauthenticationway.server.manyauthenticationway.config.handle.MyAuthenticationFailureHandler
import com.manyauthenticationway.server.manyauthenticationway.config.handle.MyAuthenticationSuccessHandler
import com.manyauthenticationway.server.manyauthenticationway.domain.user.SysUserService
import org.springframework.context.annotation.Configuration
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer


@EnableResourceServer
@Configuration
class ResourceServerConfig: ResourceServerConfigurerAdapter(){
    @Autowired
    private lateinit var myAuthenticationSuccessHandler: MyAuthenticationSuccessHandler

    @Autowired
    private lateinit var myAuthenticationFailureHandler: MyAuthenticationFailureHandler

    override fun configure(http: HttpSecurity) {
        http
                .formLogin()
                    //登录页面，app用不到
                    //.loginPage("/authentication/login")
                    //登录提交action，app会用到 用户名登录地址
                    .loginProcessingUrl("/form/token")
                    //成功处理器 返回Token
                    .successHandler(myAuthenticationSuccessHandler)
                    //失败处理器
                    .failureHandler(myAuthenticationFailureHandler)
                .and()
                //模拟请求
                    .csrf().disable()
                    .cors()
                .and()
                    .authorizeRequests().anyRequest().authenticated()
                .and()
                    .httpBasic().disable()
    }

    override fun configure(resources: ResourceServerSecurityConfigurer?) {
        super.configure(resources)
    }
}